package com.ccp.dev.form.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.BeanCopyUtil;
import com.ccp.dev.form.dao.FormDefDao;
import com.ccp.dev.form.dao.FormDefHiDao;
import com.ccp.dev.form.model.FormDef;
import com.ccp.dev.form.model.FormDefHi;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * BPM_FORM_DEF_HI service类
 *
 * @author zhaowj
 */
@Service
public class FormDefHiService extends BaseService<FormDefHi> {

    @Resource
    FormDefHiDao formDefHiDao;

    @Resource
    FormDefDao formDefDao;

    /**
     * 恢复表单记录
     *
     * @param formDefHi
     */
    @Transactional(rollbackFor = Exception.class)
    public void recoveryData(FormDefHi formDefHi) {

        //根据表单的key值获取当前表单的最大版本
        Integer rtn = formDefDao.getMaxVersionByFormKey(formDefHi.getFormKey());
        FormDef newVersion = BeanCopyUtil.createAndCopy(formDefHi, FormDef.class);
        String newFormDefId = UUIDUtils.getUUIDFor32();
        newVersion.setFormDefId(newFormDefId);
        newVersion.setIsDefault((short) 0);
        newVersion.setIsPublished((short) 0);
        newVersion.setPublishedBy("");
        newVersion.setVersionNo(rtn + 1);
        formDefDao.add(newVersion);
        formDefHiDao.delById(formDefHi.getHisId());
    }
}
